#ifndef __DBMGR_H__
#define __DBMGR_H__

#include <winsock2.h>
#include <mysql.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <time.h>

#include "protocol.h"
#include "util.h"

using namespace std;


#define MAX_SQL_LENGTH  512
#define DURATION_YEAR_CARD 0
#define DURATION_TIME_CARD 1


class DBMgr
{
private:
    MYSQL *mysql;
    MYSQL_RES *mysql_res;
    MYSQL_ROW mysql_row;

public:
    DBMgr();
    ~DBMgr();
    int InitDB();
    int HandleGetIDByMemberName(const char* name);
    int HandleAddMember(Member* pMember);
    int HandleAddCardToMember(Order* pOrder, const char* cardType);

private:
    int GetMaxMemberID();
    int GetMaxCardID();

    int InsertIntoMember(Member* pMember);
    int InsertIntoCard(Card* pCard);
    int InsertIntoOrder(Order* pOrder, const char* cardType);
    int InsertIntoTable(const char* sql_line);

    bool CheckMemberExisted(const char* memName, int& mid);

    int MysqlInit();
    int MysqlConnect();
    int MysqlClose();

    char* GetStartDate();
    char* GetEndDate(int duration);

};


#endif // __DBMGR_H__
